Imports System.Collections.Generic
Imports System.Linq
Imports System.Web

#Region "using"
Imports System.Data
Imports QUANLY.DTO
#End Region
Public Class NHANVIENDAO
	Inherits AbstractDAO
	Public Sub New(strPath As [String])
		MyBase.New(strPath)
	End Sub

	Public Function SelectAll() As DataSet
		Dim ds As DataSet
		Try
			ds = New DataSet()
			MyBase.sql = "usp_NHANVIEN_SelectAll"
			MyBase.FillDataSet(ds, "NHANVIEN")
			Return ds
		Catch e As System.Exception
			Throw New Exception(e.Message, e.InnerException)
		Finally
			MyBase.CleanUpObject()
		End Try
	End Function

	Public Function SelectByID(id As Integer) As DataSet
		Dim ds As DataSet
		Try
			ds = New DataSet()
			MyBase.sql = "usp_NHANVIEN_SelectByID"
			MyBase.InitializeCommand()
			MyBase.AddParameter("@ID", SqlDbType.Int, 1000, id)
			MyBase.FillDataSet(ds, "NHANVIEN")
			Return ds
		Catch e As System.Exception
			Throw New Exception(e.Message, e.InnerException)
		Finally
			MyBase.CleanUpObject()
		End Try
	End Function

    Public Function Search(ByVal id As String, ByVal name As String) As DataSet
        Dim ds As DataSet
        Try
            ds = New DataSet()
            MyBase.sql = "SELECT nhanvien.ID, nhanvien.IDLoaiNguoiDung, nhanvien.IDCV, nhanvien.TenNV, nhanvien.NgaySinh, nhanvien.GioiTinh, nhanvien.DiaChi, nhanvien.SoDT, loainguoidung.TenLoaiNguoiDung, chucvu.TenCV, TenDangNhap, MatKhau " & _
                        "FROM NHANVIEN AS nhanvien, LOAINGUOIDUNG AS loainguoidung, CHUCVU AS chucvu " & _
                        "WHERE nhanvien.IDLoaiNguoiDung = loainguoidung.ID AND nhanvien.IDCV = chucvu.ID And " & _
                            "nhanvien.ID = " & IIf(id = "", "nhanvien.ID", id) & " And " & _
                            "UPPER(nhanvien.TenNV) like " & IIf(name = "", "UPPER(nhanvien.TenNV)", "N'%" & name.Trim.ToUpper() & "%'")
            MyBase.InitializeCommand()
            MyBase.FillDataSet(ds, "NHANVIEN")
            Return ds
        Catch e As System.Exception
            Throw New Exception(e.Message, e.InnerException)
        Finally
            MyBase.CleanUpObject()
        End Try
    End Function

	Public Function Insert(obj As NHANVIENDTO) As Boolean
		Try
			MyBase.sql = "usp_NHANVIEN_Insert"
			MyBase.InitializeCommand()
			MyBase.AddParameter("@IDLoaiNguoiDung", SqlDbType.Int, 1000, obj.IDLoaiNguoiDung)
			MyBase.AddParameter("@IDCV", SqlDbType.Int, 1000, obj.IDCV)
			MyBase.AddParameter("@TenNV", SqlDbType.NVarChar, 1000, obj.TenNV)
			MyBase.AddParameter("@NgaySinh", SqlDbType.DateTime, 1000, obj.NgaySinh)
			MyBase.AddParameter("@GioiTinh", SqlDbType.Bit, 1000, obj.GioiTinh)
			MyBase.AddParameter("@DiaChi", SqlDbType.NVarChar, 1000, obj.DiaChi)
            MyBase.AddParameter("@SoDT", SqlDbType.NVarChar, 1000, obj.SoDT)
            MyBase.AddParameter("@TenDangNhap", SqlDbType.NVarChar, 1000, obj.TenDangNhap)
            MyBase.AddParameter("@MatKhau", SqlDbType.NVarChar, 1000, obj.MatKhau)
			Return ExecuteStoredProcedure()
		Catch
			Return False
		End Try
	End Function

	Public Function Update(obj As NHANVIENDTO) As Boolean
		Try
			MyBase.sql = "usp_NHANVIEN_Update"
			MyBase.InitializeCommand()
			MyBase.AddParameter("@ID", SqlDbType.Int, 1000, obj.ID)
			MyBase.AddParameter("@IDLoaiNguoiDung", SqlDbType.Int, 1000, obj.IDLoaiNguoiDung)
			MyBase.AddParameter("@IDCV", SqlDbType.Int, 1000, obj.IDCV)
			MyBase.AddParameter("@TenNV", SqlDbType.NVarChar, 1000, obj.TenNV)
			MyBase.AddParameter("@NgaySinh", SqlDbType.DateTime, 1000, obj.NgaySinh)
			MyBase.AddParameter("@GioiTinh", SqlDbType.Bit, 1000, obj.GioiTinh)
			MyBase.AddParameter("@DiaChi", SqlDbType.NVarChar, 1000, obj.DiaChi)
            MyBase.AddParameter("@SoDT", SqlDbType.NVarChar, 1000, obj.SoDT)
            MyBase.AddParameter("@TenDangNhap", SqlDbType.NVarChar, 1000, obj.TenDangNhap)
			Return ExecuteStoredProcedure()
		Catch
			Return False
		End Try
	End Function

	Public Function Delete(id As Integer) As Boolean
		Try
			MyBase.sql = "usp_NHANVIEN_Delete"
			MyBase.InitializeCommand()
			MyBase.AddParameter("@ID", SqlDbType.Int, 1000, id)
			Return ExecuteStoredProcedure()
		Catch
			Return False
		End Try
	End Function

	Public Function SelectByIDLoaiNguoiDung(idloainguoidung As Integer) As DataSet
		Dim ds As DataSet
		Try
			ds = New DataSet()
			MyBase.sql = "usp_NHANVIEN_SelectByIDLoaiNguoiDung"
			MyBase.InitializeCommand()
			MyBase.AddParameter("@IDLoaiNguoiDung", SqlDbType.Int, 1000, idloainguoidung)
			MyBase.FillDataSet(ds, "NHANVIEN")
			Return ds
		Catch e As System.Exception
			Throw New Exception(e.Message, e.InnerException)
		Finally
			MyBase.CleanUpObject()
		End Try
	End Function

	Public Function SelectByIDCV(idcv As Integer) As DataSet
		Dim ds As DataSet
		Try
			ds = New DataSet()
			MyBase.sql = "usp_NHANVIEN_SelectByIDCV"
			MyBase.InitializeCommand()
			MyBase.AddParameter("@IDCV", SqlDbType.Int, 1000, idcv)
			MyBase.FillDataSet(ds, "NHANVIEN")
			Return ds
		Catch e As System.Exception
			Throw New Exception(e.Message, e.InnerException)
		Finally
			MyBase.CleanUpObject()
		End Try
	End Function

    Public Function SelectByUserName(ByVal username As String) As DataSet
        Dim ds As DataSet
        Try
            ds = New DataSet()
            MyBase.sql = "usp_NHANVIEN_SelectByUserName"
            MyBase.InitializeCommand()
            MyBase.AddParameter("@UserName", SqlDbType.NVarChar, 1000, username)
            MyBase.FillDataSet(ds, "NHANVIEN")
            Return ds
        Catch e As System.Exception
            Throw New Exception(e.Message, e.InnerException)
        Finally
            MyBase.CleanUpObject()
        End Try
    End Function

    Public Function SelectByUserNameAndPassword(ByVal username As String, ByVal password As String) As DataSet
        Dim ds As DataSet
        Try
            ds = New DataSet()
            MyBase.sql = "usp_NHANVIEN_SelectByUserNameAndPassword"
            MyBase.InitializeCommand()
            MyBase.AddParameter("@UserName", SqlDbType.NVarChar, 1000, username)
            MyBase.AddParameter("@PassWord", SqlDbType.NVarChar, 1000, password)
            MyBase.FillDataSet(ds, "NHANVIEN")
            Return ds
        Catch e As System.Exception
            Throw New Exception(e.Message, e.InnerException)
        Finally
            MyBase.CleanUpObject()
        End Try
    End Function
End Class
